package cn.edu.zstu.overlappingdisappear.sqlite;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author ZhuMingWei(986990501@qq.com)
 * @Date on 2019/11/15
 * @Version v1.0.0
 * @Company TODO
 * @Describle TODO
 */

public class DBHelper  extends SQLiteOpenHelper {

    private static final String DB_NAME = "db_od.db";
    private static final int DB_VERSION = 1;
    private Context mContext;
    //数据库存储路径
    private String filePath = "data/data/cn.edu.zstu.overlappingdisappear/databases/db_od.db";
    //数据库存放的文件夹 data/data/cn.edu.zstu.overlappingdisappear 下面
    private String pathStr = "data/data/cn.edu.zstu.overlappingdisappear/databases";
    private SQLiteDatabase database;

    public DBHelper(Context context){
        //CursorFactory设置为null,使用默认值
        super(context, DB_NAME, null, DB_VERSION);
        mContext = context;
        database = openDatabase(mContext);
    }

    public  SQLiteDatabase openDatabase(Context context){
        System.out.println("filePath:"+filePath);
        File jhPath=new File(filePath);
        //查看数据库文件是否存在
        if(jhPath.exists()){
            Log.i("test", "存在数据库");
            //存在则直接返回打开的数据库
            return SQLiteDatabase.openOrCreateDatabase(jhPath, null);
        }else{
            //不存在先创建文件夹
            File path=new File(pathStr);
            Log.i("test", "pathStr="+path);
            if (path.mkdir()){
                Log.i("test", "创建成功");
            }else{
                Log.i("test", "创建失败");
            };
            try {
                //得到资源
                AssetManager am= context.getAssets();
                //得到数据库的输入流
                InputStream is=am.open(DB_NAME);
                Log.i("test", is+"");
                //用输出流写到SDcard上面
                FileOutputStream fos=new FileOutputStream(jhPath);
                Log.i("test", "fos="+fos);
                Log.i("test", "jhPath="+jhPath);
                //创建byte数组  用于1KB写一次
                byte[] buffer=new byte[1024];
                int count = 0;
                while((count = is.read(buffer))>0){
                    Log.i("test", "得到");
                    fos.write(buffer,0,count);
                }
                //最后关闭就可以了
                fos.flush();
                fos.close();
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
            //如果没有这个数据库  我们已经把他写到SD卡上了，然后在执行一次这个方法 就可以返回数据库了
            return openDatabase(context);
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
